package com.dushougudu;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LeecodeBT {

    Map<Character, String> phoneMap = new HashMap<Character, String>() {{
        put('2', "abc");
        put('3', "def");
        put('4', "ghi");
        put('5', "jkl");
        put('6', "mno");
        put('7', "pqrs");
        put('8', "tuv");
        put('9', "wxyz");
    }};

    /**
     * 电话号码组合
     * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回
     */
    @Test
    public void letterCombinations() {
        String digits = "22";
        List<String> combinations = new ArrayList<String>();

        backtrackLetter(combinations, digits, 0, new StringBuffer());
        for (String s : combinations) {
            System.out.println(s);
        }
    }

    public void backtrackLetter(List<String> combinations, String digits, int index, StringBuffer combination) {
        if (index == digits.length()) {
            combinations.add(combination.toString());
            return;
        }
        String s = phoneMap.get(digits.charAt(index));
        for (int i = 0; i < s.length(); i++) {
            combination.append(s.charAt(i));
            backtrackLetter(combinations, digits, index + 1, combination);
            combination.deleteCharAt(combination.length() - 1);
        }
    }


}
